package com.supply.dl.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.request.NoticeRequest;
import com.supply.dl.constant.Constants;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.CommonNormalAdapterUtil;
import com.supply.dl.utils.RecyclerViewUtil;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.SystemUtil;
import com.supply.dl.utils.Utils;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.CommonNormalAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ${qianzhihe} on 2018/5/10.
 * 通知单入库界面、通知单出库界面
 */

public class NoticeLetterInAndOutBoundActivity extends BaseActivity{

    @BindView(R.id.common_search_main_searchTv)
    TextView searchTv;
    @BindView(R.id.common_search_main_filterLayout)
    LinearLayout filterLayout;
    @BindView(R.id.common_search_main_searchContentEdt)
    EditText searchContentEdt;

    @BindView(R.id.notice_letter_in_and_out_bound_main_recyclerView)
    RecyclerView mRecyclerView;
    CommonNormalAdapter commonNormalAdapter;
    List<BaseItemBean> itemList;

    /**
     * 加载框
     */
    @BindView(R.id.notice_letter_in_and_out_bound_main_loadView)
    LoadTipFailedLayout loadTipFailedLayout;

    @BindView(R.id.common_search_main_filterTv)
    TextView filterTv;
    @BindView(R.id.common_search_main_filterImg)
    ImageView filterImg;

    int activityFlag;
    int mAdapterFlag=-1;
    boolean isInBound;

    @Override
    protected int getLayoutResource() {
        return R.layout.notice_letter_in_and_out_bound_main;
    }

    @Override
    protected void initView() {
        showLeftBack();
        searchContentEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length()>0){
                    searchContentEdt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                }else {
                    searchContentEdt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        searchTv.setOnClickListener(this);
        filterLayout.setOnClickListener(this);
        setRightHomeClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishAllActivityExceptAppointed(MainActivity.class);
            }
        });

        setRightScanClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startScanActivity(NoticeLetterInAndOutBoundActivity.this);
            }
        });
    }

    @Override
    protected void initLoadingView() {
        super.initLoadingView();
        loadTipFailedLayout.setRestartOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI){
                    isInBound=true;
                }else if(activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_OUT_BOUND_UI){
                    isInBound=false;
                }
                getNoticeInOrOutBoundList(isInBound);
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        itemList=new ArrayList<>();
        activityFlag=getIntent().getIntExtra(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI);
        if (activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI){
            setTitleShow(getResourcesString(R.string.notice_to_inbound));
            mAdapterFlag=CommonNormalAdapter.FLAG_NOTICE_LETTER_IN_BOUND_MAIN;
            isInBound=true;
        }else if(activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_OUT_BOUND_UI){
            setTitleShow(getResourcesString(R.string.notice_to_out_outbound));
            mAdapterFlag=CommonNormalAdapter.FLAG_SEND_LETTER_OUT_BOUND_MAIN;
            isInBound=false;
        }
        getNoticeInOrOutBoundList(isInBound);
    }

    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();
        RecyclerViewUtil.initFullLinearLayoutVERTICAL(this, mRecyclerView);
        commonNormalAdapter= CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.inbound_location_main_item, itemList, mAdapterFlag);
        commonNormalAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                if (activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI){
                    bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_NOTICE_IN_BOUND_DETAILS_UI);
                }else {
                    bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_NOTICE_OUT_BOUND_DETAILS_UI);
                }
                bundle.putSerializable("InAndOutBoundLocationBean", (InAndOutBoundLocationBean)itemList.get(position).object);
                jumpActivity(NoticeInAndOutboundDetailsActivity.class, bundle);
            }
        });
        View notDataView = getLayoutInflater().inflate(R.layout.empty_view_main_textview, (ViewGroup) mRecyclerView.getParent(), false);
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        notDataView.setLayoutParams(layoutParams);
//        commonNormalAdapter.setEmptyView(notDataView);
        mRecyclerView.setAdapter(commonNormalAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.common_search_main_searchTv:
                if (StringUtils.isEmpty(searchContentEdt.getText().toString())){
                    ToastUtil.showToast(this, getResourcesString(R.string.toastInputContentNotEmpty));
                    return;
                }
                if (activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI){
                    isInBound=true;
                }else if(activityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_OUT_BOUND_UI){
                    isInBound=false;
                }
                getNoticeInOrOutBoundList(isInBound);
                SystemUtil.hideSoftInput(NoticeLetterInAndOutBoundActivity.this, searchContentEdt);
                break;
            case R.id.common_search_main_filterLayout:
                filterTv.setTextColor(getResources().getColor(R.color.blue));
                filterImg.setImageResource(R.mipmap.ic_filter_seleted);
                SystemUtil.hideSoftInput(NoticeLetterInAndOutBoundActivity.this, searchContentEdt);
                break;
        }
    }

    /**
     * 获取通知单入库、出库列表数据
     */
    private void getNoticeInOrOutBoundList(boolean isInBound){
        loadTipFailedLayout.setLoadText(R.string.since_help_loading);
        loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
        HttpManger httpManger=HttpManger.getInstance();
        httpManger.setContext(this);
        NoticeRequest noticeRequest=new NoticeRequest();
        noticeRequest.key=searchContentEdt.getText().toString();
        noticeRequest.displayNodOrderStatus="";
        noticeRequest.bussType="";
        noticeRequest.companyId="";
        noticeRequest.factoryId="";
        noticeRequest.productWorkshopId="";
        noticeRequest.createTimeStart="";
        noticeRequest.createTimeEnd="";
        if (isInBound){
            httpManger.getNoticeInBoundList(noticeRequest, "1");
        }else {
            httpManger.getNoticeOutBoundList(noticeRequest, "1");
        }
        httpManger.setHttpResponseCallBackListener(this);
    }

    @Override
    public void onSuccess(BaseInfo<?> baseInfo) {
        super.onSuccess(baseInfo);
        List<InAndOutBoundLocationBean> dataList= (List<InAndOutBoundLocationBean>) baseInfo.getData();
        itemList.clear();
        if (null!=dataList && dataList.size()>0){
            for (InAndOutBoundLocationBean locationBean : dataList){
                itemList.add(new BaseItemBean(locationBean));
            }
            loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
        }else {
            loadTipFailedLayout.setTextColor(R.color.color_gray);
            loadTipFailedLayout.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
            loadTipFailedLayout.setNormalText(R.string.noMessage);
            loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
        }
        commonNormalAdapter.setNewData(itemList);
    }

    @Override
    public void onFailure(String message) {
        super.onFailure(message);
        loadTipFailedLayout.setTextColor(R.color.color_gray);
        loadTipFailedLayout.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        loadTipFailedLayout.setNormalText(R.string.since_help_load_failed);
        loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
        ToastUtil.showToast(NoticeLetterInAndOutBoundActivity.this, message);
    }
}
